package com.compton.UI
{
	import com.compton.EVENT.WineEvent;
	import com.compton.VO.WineVO;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	
	public class WineServices extends EventDispatcher
	{
		private var _keyWord:String = "Red Wine";
		private var _wineArray:Array = [];
		
		public function WineServices(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		//recieve xml will take the variables passed in and start loading the xml and recieve a response from the API
		public function recieveXML(keyword:String,nResults:String,minPrice:String, maxPrice:String, country:String):void
		{
			if(keyword == ""){
				returnError();
			}else{
				//trace(zipcode);
				var wineLoader:URLLoader = new URLLoader();
				var wineRequest:URLRequest = new URLRequest();
				var wineVariables:URLVariables = new URLVariables();
				
				wineVariables.akey = WineConstInfo.WINE_KEY;
				wineVariables.format = "xml";
				wineVariables.t = "wine";
				wineVariables.q = keyword;
				wineVariables.n = nResults;
				wineVariables.s = "sr";
				wineVariables.mp = minPrice;
				wineVariables.xp = maxPrice;
				//wineVariables.z = zipcode;
				wineVariables.c = country;
				
				wineRequest.data = wineVariables;
				wineRequest.url = "https://api.snooth.com/wines/";
				
				wineLoader.addEventListener(Event.COMPLETE, onDataLoaded);
				wineLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
				
				wineLoader.load(wineRequest);
			}
		}
		
		//after data is loaded the data is parsed and pushed into an array to be cleared out if the program wants to run another search
		private function onDataLoaded(evt:Event):void
		{
			var wineXML:XML = new XML(evt.currentTarget.data);
			while(_wineArray.length > 0)
			{
				_wineArray.pop();	
			}	
			
			for each(var wine:XML in wineXML.wines.wine)
			{
				var wines:WineVO = new WineVO();
				wines.name = wine.name;
				wines.winery = wine.winery;
				wines.varietal = wine.varietal;
				wines.price = wine.price;
				wines.vintage = wine.vintage;
				wines.type = wine.type;
				wines.link = wine.link;
				wines.image = wine.image;
				parseFloat(wine.snoothrank); 
				wines.ranking = wine.snoothrank;
				wines.numberMerchs = wine.num_merchants;
				_wineArray.push(wines);
			
				if(wines.image == "")
				{
					wines.image = "http://em1.isnooth.com/img/site/default_wine_87.png";	
				}
			}
			var e:WineEvent = new WineEvent(WineEvent.ADD_WINES);
			e.wineArray = _wineArray;
			this.dispatchEvent(e);
		}
		
		//runs the error function if their is a problem loading the data
		private function onError(evt:IOErrorEvent):void
		{
			returnError();
		}
		
		//dispatched the error event for the main to listen too and respond accordingly
		private function returnError():void
		{
			var e:WineEvent = new WineEvent(WineEvent.WINE_SEARCH_ERROR);
			e.errorMessage = "error";
			this.dispatchEvent(e);
		}
	}
}